<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>对某些网站进行自动签到 | Miles19 | LIFE WITHOUT LOOPS</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Python,Selenium">
    <meta name="description" content="在服务器上部署一个定时任务：在每天的00:15点击一个网站的按钮，进行自动签到。 模拟浏览器的点击按钮操作1234567891011121314151617181920212223242526272829303132333435363738394041424344from selenium import webdriverdef autoCheck():    print(&quot;------开始执行自">
<meta property="og:type" content="article">
<meta property="og:title" content="对某些网站进行自动签到">
<meta property="og:url" content="https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/index.html">
<meta property="og:site_name" content="Miles19">
<meta property="og:description" content="在服务器上部署一个定时任务：在每天的00:15点击一个网站的按钮，进行自动签到。 模拟浏览器的点击按钮操作1234567891011121314151617181920212223242526272829303132333435363738394041424344from selenium import webdriverdef autoCheck():    print(&quot;------开始执行自">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-07-18T04:45:42.427Z">
<meta property="article:modified_time" content="2020-07-18T04:48:37.386Z">
<meta property="article:author" content="Miles Wu">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="Selenium">
<meta name="twitter:card" content="summary">
    
    <link rel="shortcut icon" href="/myblog/img/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>
    

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/myblog/img/brand.jpg)">
      <div class="brand">
        <a href="/myblog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/myblog/img/avatar.png">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Miles Wu</h5>
          <a href="mailto:wu5040@gmail.com" title="wu5040@gmail.com" class="mail">wu5040@gmail.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/myblog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/myblog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/myblog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/myblog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">对某些网站进行自动签到</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">对某些网站进行自动签到</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-07-18T04:45:42.427Z" itemprop="datePublished" class="page-time">
  2020-07-18
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/myblog/categories/%E8%87%AA%E5%8A%A8%E5%8C%96/">自动化</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#模拟浏览器的点击按钮操作"><span class="post-toc-number">1.</span> <span class="post-toc-text">模拟浏览器的点击按钮操作</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#使用crontab配置定时任务"><span class="post-toc-number">2.</span> <span class="post-toc-text">使用crontab配置定时任务</span></a></li></ol>
        </nav>
    </aside>


<article id="post-对某些网站进行自动签到"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">对某些网站进行自动签到</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-07-18 12:45:42" datetime="2020-07-18T04:45:42.427Z"  itemprop="datePublished">2020-07-18</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/myblog/categories/%E8%87%AA%E5%8A%A8%E5%8C%96/">自动化</a></li></ul>



            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>在服务器上部署一个定时任务：在每天的00:15点击一个网站的按钮，进行自动签到。</p>
<h2 id="模拟浏览器的点击按钮操作"><a href="#模拟浏览器的点击按钮操作" class="headerlink" title="模拟浏览器的点击按钮操作"></a>模拟浏览器的点击按钮操作</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">autoCheck</span><span class="params">()</span>:</span></span><br><span class="line">    print(<span class="string">"------开始执行自动签到程序------"</span>)</span><br><span class="line">    chrome_options = webdriver.ChromeOptions()</span><br><span class="line">    chrome_options.add_argument(<span class="string">'--no-sandbox'</span>)</span><br><span class="line">    chrome_options.add_argument(<span class="string">'--disable-dev-shm-usage'</span>)</span><br><span class="line">    chrome_options.add_argument(<span class="string">'--headless'</span>)</span><br><span class="line">    chrome_options.add_argument(<span class="string">'--disable-gpu'</span>)</span><br><span class="line">    chrome_options.add_argument(</span><br><span class="line">        <span class="string">"user-agent='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'"</span>)</span><br><span class="line">		</span><br><span class="line">    <span class="comment"># chromedriver需到官网去下载，executable_path使用绝对路径</span></span><br><span class="line">    driver = webdriver.Chrome(</span><br><span class="line">        executable_path=<span class="string">'/home/wsg/autoCheck/chromedriver'</span>, chrome_options=chrome_options)</span><br><span class="line">		</span><br><span class="line">    <span class="comment"># 先get一下url，以便添加cookie</span></span><br><span class="line">    driver.get(<span class="string">'https://glados.work/'</span>)</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 添加cookie，一般作为登录认证</span></span><br><span class="line">    cookie1 = &#123;</span><br><span class="line">        <span class="string">'name'</span>: <span class="string">''</span>,</span><br><span class="line">        <span class="string">'value'</span>: <span class="string">''</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    driver.add_cookie(cookie1)</span><br><span class="line">		</span><br><span class="line">    <span class="comment"># 请求需要进行操作的页面</span></span><br><span class="line">    driver.get(<span class="string">'https://glados.work/console/checkin'</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">try</span>: <span class="comment"># 获取按钮</span></span><br><span class="line">        button = driver.find_element_by_class_name(<span class="string">'ui.positive.button'</span>)</span><br><span class="line">    <span class="keyword">except</span>:</span><br><span class="line">        driver.close()</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"cookie信息失效"</span></span><br><span class="line">    <span class="keyword">else</span>: <span class="comment"># 进行点击按钮操作，并加上相应的判断，测试操作是否成功</span></span><br><span class="line">        print(<span class="string">"------cookie有效，开始签到------"</span>)</span><br><span class="line">        button.click()</span><br><span class="line">        time.sleep(<span class="number">10</span>)</span><br><span class="line">        res = driver.find_element_by_class_name(<span class="string">'ui.primary.button'</span>)</span><br><span class="line">        driver.close()</span><br><span class="line">        <span class="keyword">if</span>(res):</span><br><span class="line">            <span class="keyword">return</span> <span class="string">"自动签到成功"</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="string">"签到失败"</span></span><br></pre></td></tr></table></figure>

<p><strong>注意：</strong> 上述代码在本地测试时，会自动运行Chrome浏览器并进行相应的后续操作。在服务器上运行时，需要先安装Chrome浏览器。</p>
<h2 id="使用crontab配置定时任务"><a href="#使用crontab配置定时任务" class="headerlink" title="使用crontab配置定时任务"></a>使用crontab配置定时任务</h2><p>在服务器上使用命令 <code>crontab -e</code>进行编辑界面，并添加一行：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">15 00 * * * python3 &#x2F;home&#x2F;wsg&#x2F;autoCheck&#x2F;jiaoben.py</span><br></pre></td></tr></table></figure>

<p>该命令为在每天的00:15使用python执行 jiaoben.py 文件。</p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-07-18T04:48:37.386Z" itemprop="dateUpdated">2020-07-18 12:48:37</time>
</span><br>


        
    </div>
    
    <footer>
        <a href="https://wu5040.gitee.io/myblog">
            <img src="/myblog/img/avatar.png" alt="Miles Wu">
            Miles Wu
        </a>
    </footer>
</blockquote>

        


        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myblog/tags/Python/" rel="tag">Python</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myblog/tags/Selenium/" rel="tag">Selenium</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/&title=《对某些网站进行自动签到》 — Miles19&pic=https://wu5040.gitee.io/myblog/img/avatar.png" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/&title=《对某些网站进行自动签到》 — Miles19&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《对某些网站进行自动签到》 — Miles19&url=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/&via=https://wu5040.gitee.io/myblog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between flex-row-reverse">
  

  
    <div class="waves-block waves-effect next">
      <a href="/myblog/2020/06/20/%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB%E9%A1%B9%E7%9B%AE%E5%88%86%E5%88%AB%E8%BF%9B%E8%A1%8CHTTPS%E9%83%A8%E7%BD%B2/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">前后端分离项目分别进行HTTPS部署</h4>
      </a>
    </div>
  
</nav>



    




















</article>



</div>

        <footer class="footer">
    <div class="top">
        

        <p>
            
            <span> </span>
        </p>
    </div>
    <div class="bottom">
        <p><span>Miles Wu &copy; 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/&title=《对某些网站进行自动签到》 — Miles19&pic=https://wu5040.gitee.io/myblog/img/avatar.png" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/&title=《对某些网站进行自动签到》 — Miles19&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《对某些网站进行自动签到》 — Miles19&url=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/&via=https://wu5040.gitee.io/myblog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://wu5040.gitee.io/myblog/2020/07/18/%E5%AF%B9%E6%9F%90%E4%BA%9B%E7%BD%91%E7%AB%99%E8%BF%9B%E8%A1%8C%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/myblog/', SHARE: true, REWARD: false };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>



<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
    }
});
</script>

<script async src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML" async></script>








</body>
</html>
